package com.ybkj.o2o.mc.manager.impl;

import com.ybkj.o2o.mc.common.DataGrid;
import com.ybkj.o2o.mc.common.transport.PassengerConcatsResult;
import com.ybkj.o2o.mc.entity.PassengerConcats;
import com.ybkj.o2o.mc.manager.PassengerConcatsManager;
import com.ybkj.o2o.mc.service.PassengerConcatsService;
import com.ybkj.o2o.mc.service.PassengerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by shine on 15/8/19.
 */
@Service
public class DefaultPassengerConcatsManager implements PassengerConcatsManager {

  @Autowired
  private PassengerConcatsService passengerConcatsService;
  @Autowired
  private PassengerService passengerService;

  @Override
  public boolean exists(String phone, Long passengerId) {

    return passengerConcatsService.exists(phone, passengerId);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public PassengerConcats insert(PassengerConcats concats) {

    passengerConcatsService.insert(concats);

    return concats;
  }

  @Override
  public PassengerConcats findByConcatsAndPassenger(Long concatsId, Long passengerId) {

    return passengerConcatsService.findByConcatsAndPassenger(concatsId, passengerId);
  }

  @Override
  public int deleteByIdAndPassengerId(Long concatsId, Long passengerId) {

    return passengerConcatsService.deleteByIdAndPassengerId(concatsId, passengerId);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(PassengerConcats concats) {

    return passengerConcatsService.update(concats);
  }

  @Override
  public List<PassengerConcats> findByPassengerId(Long passengerId) {

    return passengerConcatsService.findByPassengerId(passengerId);
  }

  @Override
  public DataGrid<PassengerConcatsResult> findByPassengerForPage(Long passengerId,
                                                                 int page,
                                                                 int rows) {

    int offset = (page - 1) * rows;
    List<PassengerConcats> concatses = passengerConcatsService.findAll(passengerId, offset, rows);

    long count = passengerConcatsService.count(passengerId);

    DataGrid<PassengerConcatsResult> dataGrid = new DataGrid<>();
    dataGrid.setRows(PassengerConcatsResult.convertFrom(concatses));
    dataGrid.setTotal(count);

    return dataGrid;

  }


}
